package com.sa07.role.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.sa07.bean.Role;
import com.sa07.db.Session;
import com.sa07.db.dao.H2BaseDao;
import com.sa07.exception.DatabaseExecuteFailedException;

/**
 * 角色Dao
 * 
 * @author Long.shangbo
 *
 * 2014-2-21 下午3:35:36
 */
public class RoleDao extends H2BaseDao implements IRoleDao{

	@Override
	public List<Role> queryAllRoles() throws DatabaseExecuteFailedException{
		Session session = createSession();
		String sql = "select r.role_id, r.role_name from so_role r";
		ResultSet rs = session.executeQuery(sql, new Object[]{});
		
		List<Role> roleList = new ArrayList<Role>();
		Role role = null;
		
		try {
			while(rs.next()){
				role = new Role();
				role.setRoleId(rs.getInt(1));
				role.setRoleName(rs.getString(2));
				roleList.add(role);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error("Query role failed, caused by -> " + e);
		} finally{
			session.close();
		}
		
		return roleList;
	}

}
